﻿using AnimatingHair.Entity.PhysicalEntity;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System;
using System.Drawing;

namespace AnimatingHair.Rendering
{
    class BustRenderer
    {
        private static float[] skinColor = { 0.93f, 0.76f, 0.66f, 1 };

        private readonly Bust bust;
        private TriangleMesh femaleHead;
        private int eyeballTexture;

        public BustRenderer( Bust bust )
        {
            this.bust = bust;

            femaleHead = Utility.LoadOBJ( @"../../_Data/Models/hlava.obj" );
            eyeballTexture = Utility.UploadTexture( @"../../_Data/Models/eyeball.png" );
        }

        public void Render()
        {
            GL.Color3( Color.Black );


            GL.PushAttrib( AttribMask.AllAttribBits );
            GL.Material( MaterialFace.Front, MaterialParameter.Specular, new[] { 0.1f, 0.05f, 0.05f } );
            GL.Material( MaterialFace.Front, MaterialParameter.Shininess, 5 );
            GL.Material( MaterialFace.Front, MaterialParameter.Diffuse, skinColor );
            GL.Material( MaterialFace.Front, MaterialParameter.Ambient, skinColor );
            GL.PushMatrix();
            GL.Translate( (Vector3)bust.Position );
            GL.Translate( 0, -0.62, -0.28 );
            const double scale = 0.88;
            GL.Scale( scale, scale, scale );
            femaleHead.Draw();
            GL.PopMatrix();
            GL.PopAttrib();
            return;

            GL.Color3( Color.Yellow );
            renderHead();
            renderNeck();
            renderShoulders();
        }

        private void renderHead()
        {
            GL.PushMatrix();
            GL.Translate( (Vector3)bust.Head.Center );
            Utility.DrawSphere( bust.Head.Radius, 100, 100 );
            GL.PopMatrix();
        }

        private void renderNeck()
        {
            Utility.RenderCylinder(
                bust.Neck.Endpoint1.X,
                bust.Neck.Endpoint1.Y,
                bust.Neck.Endpoint1.Z,
                bust.Neck.Endpoint2.X,
                bust.Neck.Endpoint2.Y,
                bust.Neck.Endpoint2.Z,
                bust.Neck.Radius,
                100 );
        }

        private void renderShoulders()
        {
            GL.PushMatrix();
            GL.Translate( (Vector3)bust.ShoulderTipLeft.Center );
            Utility.DrawSphere( bust.ShoulderTipLeft.Radius, 50, 50 );
            GL.PopMatrix();

            GL.PushMatrix();
            GL.Translate( (Vector3)bust.ShoulderTipRight.Center );
            Utility.DrawSphere( bust.ShoulderTipRight.Radius, 50, 50 );
            GL.PopMatrix();

            Utility.RenderCylinder(
                bust.Shoulders.Endpoint1.X,
                bust.Shoulders.Endpoint1.Y,
                bust.Shoulders.Endpoint1.Z,
                bust.Shoulders.Endpoint2.X,
                bust.Shoulders.Endpoint2.Y,
                bust.Shoulders.Endpoint2.Z,
                bust.Shoulders.Radius,
                100 );
        }
    }
}
